//RenewInsureClearingModel
//2020-08-28 16:27:29

package models

import (
	"git.900sui.cn/kc/base/common/functions"
	"git.900sui.cn/kc/base/common/models/base"
	"git.900sui.cn/kc/kcgin/orm"
	"git.900sui.cn/kc/rpcinterface/interface/pay"
)

//表结构体
type RenewInsureClearingModel struct {
	Model *base.Model
	Field RenewInsureClearingModelField
}

//表字段
type RenewInsureClearingModelField struct {
	T_table           string `default:"renew_insure_clearing"`
	F_id              string `default:"id"`
	F_renew_insure_id string `default:"renew_insure_id"`
	F_bill_date       string `default:"bill_date"`
	F_amount          string `default:"amount"`
	F_status          string `default:"status"`
	F_agent_type      string `default:"agent_type"`
	F_agent_id        string `default:"agent_id"`
	F_create_time     string `default:"create_time"`
}

//初始化
func (r *RenewInsureClearingModel) Init(ormer ...orm.Ormer) *RenewInsureClearingModel {
	functions.ReflectModel(&r.Field)
	r.Model = base.NewModel(r.Field.T_table, ormer...)
	return r
}

//新增数据
func (r *RenewInsureClearingModel) Insert(data map[string]interface{}) int {
	result, _ := r.Model.Data(data).Insert()
	return result
}

// 根据bus_id和billDate查询
func (r *RenewInsureClearingModel) FindByBusIdAndBillDate(insureId, billDate int) map[string]interface{} {
	result := r.Model.Where(map[string]interface{}{
		r.Field.F_renew_insure_id: insureId,
		r.Field.F_bill_date:       billDate,
	}).Find()
	return result
}

// 根据bus_id和billDate更新
func (r *RenewInsureClearingModel) UpdateByInsureIdAndBillDate(insureId, billDate int, data map[string]interface{}) (int, error) {
	return r.Model.Where(map[string]interface{}{
		r.Field.F_renew_insure_id: insureId,
		r.Field.F_bill_date:       billDate,
	}).Data(data).Update()
}

// 根据id和status更新
func (r *RenewInsureClearingModel) UpdateByIdAndStatus(id, status interface{}, data map[string]interface{}) (int, error) {
	return r.Model.Where(map[string]interface{}{
		r.Field.F_id:     id,
		r.Field.F_status: status,
	}).Data(data).Update()
}

// 根据renewInsureId,agentId，agentType，status更新数据
func (r *RenewInsureClearingModel) UpdateAgent(renewInsureId, agentId, agentType, status interface{}, data map[string]interface{}) (int, error) {
	return r.Model.Where(map[string]interface{}{
		r.Field.F_renew_insure_id: renewInsureId,
		r.Field.F_agent_id:        agentId,
		r.Field.F_agent_type:      agentType,
		r.Field.F_status:          status,
	}).Data(data).Update()
}

// 根据状态获取分页数据
func (r *RenewInsureClearingModel) Lists(billDate string, start, limit int) []map[string]interface{} {
	return r.Model.Where(map[string]interface{}{
		r.Field.F_status:    pay.CLEAR_STATUS_none,
		r.Field.F_bill_date: []interface{}{"lt", billDate},
	}).Limit(start, limit).OrderBy(r.Field.F_renew_insure_id).Select()
}
